package de.ubschmidt.capi;

import de.ubschmidt.capi.util.StringUtil;
import java.nio.ByteBuffer;

/**
 *
 */
public class TypedNumberWithPresentationIndicator
        extends TypedNumber
        implements ByteBufferWritable {

    private byte _presentationAndScreeningIndicator = (byte) 0x80;

    /** Creates a new instance of CallingPartyNumber. */
    public TypedNumberWithPresentationIndicator() {
        setType((byte) 0x00);
    }

    /**
     *
     * @param byteBuffer
     */
    public TypedNumberWithPresentationIndicator(final ByteBuffer byteBuffer) {
        byte [] buf = CapiStruct.readStructContent(byteBuffer);

        if (buf.length >= 1) {
            setType(buf[0]);
            setPresentationAndScreeningIndicator(buf[1]);
            setNumber(StringUtil.string(buf, 2));
        }
    }

    /**
     *
     * @return
     */
    public byte getPresentationAndScreeningIndicator() {
        return _presentationAndScreeningIndicator;
    }

    /**
     *
     * @param presentationAndScreeningIndicator
     */
    public void setPresentationAndScreeningIndicator(
            final byte presentationAndScreeningIndicator) {
        _presentationAndScreeningIndicator = presentationAndScreeningIndicator;
    }

    /**
     * The binary representation of a {@code CallingPartyNumber}
     * is similar to that of a {@code TypedNumber}. The difference is the
     * additional {@code presentation and screening indicator}.
     *
     * {@code CallingPartyNumber}.
     */
    @Override
    public void writeTo(final ByteBuffer byteBuffer) {
        String number = getNumber();

        if (number == null) {
            // empty struct
            byteBuffer.put((byte) 0);
        } else {
            int length = 2 + number.length();

            CapiStruct.writeStructLength(byteBuffer, length);
            byteBuffer.put(getType());
            byteBuffer.put(getPresentationAndScreeningIndicator());
            byteBuffer.put(StringUtil.byteBuf(getNumber()));

        }
    }

}
